export default {
  namespaced: true,

  state: {
    token: uni.getStorageSync('token') || 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo',
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    userInfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
    defaultAvatar: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
    defaultNickName: '未知用户',
    redirectInfo:null,
  },

  mutations: {
    //更新收获地址
    showAddress(state, success) {
      state.address = success;
      this.commit('m_user/saveAddressToStorage');
    },

    //将用户选择的收获地址持久化存储到本地
    saveAddressToStorage(state) {
      uni.setStorageSync('address', JSON.stringify(state.address));
    },

    //保存用户信息
    updateAvatar(state, avatar) {
      state.userInfo.avatar = avatar;
      this.commit('m_user/saveUserInfoToLocal');
    },
    updateNickName(state,name){
      state.userInfo.nickName=name;
      this.commit('m_user/saveUserInfoToLocal');
    },
    //将用户信息存储在本地
    saveUserInfoToLocal(state) {
      uni.setStorageSync('userinfo', JSON.stringify(state.userInfo));
    },

    //更新token
    updateToken(state, token) {
      state.token = token;
      this.commit('m_user/saveTokenToStorage');
    },

    //将token保存到本地
    saveTokenToStorage(state) {
      uni.setStorageSync('token', state.token);
    },
    
    //更新redirectInfo
    updateRedirectInfo(state,redirInfo){
      state.redirectInfo=redirInfo;
    },
  },

  getters: {
    addStr(state) {
      if (!state.address.provinceName) return '';
      return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo;
    },
  },
}